package source.model;

import java.util.Hashtable;
import source.view.MainScreen;
import source.Driver;

public class OwnershipMap {
	private Hashtable<String, Integer> lib;
	private IdList[] grid;
	
	public OwnershipMap() {
		grid = new IdList[Driver.numPlayers];
		for ( int i = 0; i < Driver.numPlayers; i++) 
		{
				grid[i] = new IdList();
		}
		
		lib = new Hashtable<String, Integer>();
	}
		
	public boolean addGameObject( GameObject go, int pID ) {
		if(!go.isPlayerOwned())
		{
			System.out.println("GameObject is not player owned");
			return false;
		}
		lib.put(go.objectID().getID(), pID);
		return grid[pID].add(go.objectID());
	}
	
	public boolean removeObject( ObjectID id ) {
		Integer pid = lib.remove(id.getID());
		if (pid != null) {
		return grid[pid].remove(id);
		}
		return false;
	}
	
	public boolean removeObject( GameObject go ) {
		return removeObject( go.objectID() );
	}
	
	public int getPlayerID( ObjectID id ) {
		return lib.get(id.getID());
	}
	
	public int getPlayerID( GameObject go ) {
		return getPlayerID( go.objectID() );
	}
	
	public ObjectID[] getPlayersEntities( int pID ) {
		ObjectID[] o = grid[pID].getIds();
		if(o == null)
		{
			MainScreen.writeToConsole("OwnershipMap:  Current Player has no entities");
			return null;
		}
		o = ObjectID.sortDecending(o);
		return o;
	}
}
